//
//  YJDUserPortrait.m
//  YJDSDK
//
//  Created by skyline on 2018/3/7.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "YJDUserPortrait.h"
CGFloat const HEIGHT = 50;
@interface YJDUserPortrait()
@property (nonatomic, strong) YJDImageView *portraitIMG;
@property (nonatomic, strong) YJDLabel *nameLBL;
@property (nonatomic, strong) YJDCustomButton *switchBtn;
@end
@implementation YJDUserPortrait
- (instancetype)initWithName:(NSString *)name
{
    self = [super init];
    if (self)
    {
        self.nameLBL.text = [NSString stringWithFormat:@"ID:%@",name];
        self.switchBtn.hidden = NO;
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (YJDImageView *)portraitIMG
{
    if (!_portraitIMG)
    {
        _portraitIMG = [[YJDImageView alloc] initWithImageName:@"user"];
        _portraitIMG.bounds = CGRectMake(0, 0, HEIGHT - GAP, HEIGHT - GAP);
        _portraitIMG.layer.cornerRadius = _portraitIMG.bounds.size.width / 2.0;
        _portraitIMG.layer.masksToBounds = YES;
        [self addSubview:_portraitIMG];
        [_portraitIMG mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(self).with.offset(GAP);
            make.size.mas_equalTo(_portraitIMG.bounds.size);
        }];
    }
    return _portraitIMG;
}

- (YJDLabel *)nameLBL
{
    if (!_nameLBL)
    {
        _nameLBL = [[YJDLabel alloc] init];
        _nameLBL.font = [UIFont systemFontOfSize:15];
        _nameLBL.textColor = [UIColor blackColor];
        [self addSubview:_nameLBL];
        [_nameLBL mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(self.portraitIMG.mas_right).with.offset(GAP);
        }];
    }
    return _nameLBL;
}

- (YJDCustomButton *)switchBtn
{
    if (!_switchBtn)
    {
        _switchBtn = [[YJDCustomButton alloc] initWithTitle:@"切换账号"];
        [_switchBtn addTarget:self action:@selector(yjd_actionForswitchBtn) forControlEvents:UIControlEventTouchUpInside];
        [_switchBtn setTitleColor:[YJDHelper shareHelp].color forState:UIControlStateNormal];
        [_switchBtn setTitleColor:TEXT_GRAY_COLOR forState:UIControlStateHighlighted];
        _switchBtn.layer.borderColor = [YJDHelper shareHelp].color.CGColor;
        _switchBtn.layer.borderWidth = 0.5;
        [self addSubview:_switchBtn];
        
        [_switchBtn mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.right.equalTo(self).with.offset(- GAP);
            make.size.mas_equalTo(_switchBtn.bounds.size);
        }];
    }
    return _switchBtn;
}

- (void)yjd_actionForswitchBtn
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(yjd_methodForSwitchButton)])
    {
        [self.delegate yjd_methodForSwitchButton];
    }
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
